  if(localStorage.notifications == undefined)
  {
	localStorage.notifications = "";
  }
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello"){
		var msg = localStorage.notifications;
		localStorage.notifications = "";
		sendResponse({farewell: msg});
	}
    else
      sendResponse({}); // snub them.
  });
var req = new XMLHttpRequest();
	chrome.tabs.onUpdated.addListener(function(tabId, props) {
	  console.log('tabs.onUpdated -- tab: ' + tabId + ' url ' + props.url);
	  if(props.url!=undefined) {
		  //console.log(getExpToNextLevel(localstorage.level));
		  visitedNewWebsite();
	  }
	});

function visitedNewWebsite() {
	if(localStorage["pt_pokemon"]==undefined) return;
	
	if(determineEncounter()) {
		var pt_items = localStorage["pt_items"];
		if(pt_items!=undefined) {
			pt_items = JSON.parse(localStorage["pt_items"]);
			var poison = Math.random();
			if(poison<0.2) {
				var pt_items = JSON.parse(localStorage["pt_items"]);
				if(pt_items[0] != undefined) {
					localStorage.notifications = localStorage.notifications + "|Oh no, you are now poisoned, but luckily you had an antidote!";
				}
				pt_items[0] = undefined;
				localStorage["pt_items"] = JSON.stringify(pt_items);
			}
		}
		var pt_history = localStorage["pt_history"];
		if(pt_history!=undefined) {
			pt_history = JSON.parse(localStorage["pt_history"]);
		}
		var myPokemon = new pokemon(localStorage["pt_pokemon"], localStorage["pt_level"], 
									localStorage["pt_strength"], localStorage["pt_speed"],
									localStorage["pt_hpmax"], localStorage["pt_exp"], 
									localStorage["pt_expToNextLevel"], 
									localStorage["pt_money"],  pt_items, pt_history); 
		var randPokemon = generateRandomPokemon(myPokemon);
		console.log("Pokemon: "+myPokemon.id
				   +" Level: "+myPokemon.level
				   +" Strength: "+myPokemon.strength
				   +" Speed: "+myPokemon.speed
				   +" HP: "+myPokemon.hpmax
				   +" EXP: "+myPokemon.exp
				   + "EXPTNL: "+myPokemon.expToNextLevel
				   +" Money: "+myPokemon.money);
		console.log("RPokemon: "+randPokemon.id
				   +" RLevel: "+randPokemon.level
				   +" RStrength: "+randPokemon.strength
				   +" RSpeed: "+randPokemon.speed
				   +" RHP: "+randPokemon.hpmax
				   +" REXP: "+randPokemon.exp
				   + "REXPTNL: "+randPokemon.expToNextLevel
				   +" RMoney: "+randPokemon.money);
		var result = battle(myPokemon, randPokemon);
		if(result == true)
		{
			localStorage.notifications = localStorage.notifications + "|You have won a battle.";
			myPokemon.exp = parseInt(myPokemon.exp) + parseInt(randPokemon.exp);
			localStorage["pt_exp"] = myPokemon.exp;
			myPokemon.expToNextLevel = myPokemon.expToNextLevel - randPokemon.exp;
			localStorage["pt_expToNextLevel"] = myPokemon.expToNextLevel;
			myPokemon.money = parseInt(myPokemon.money) + parseInt(randPokemon.money);
			localStorage["pt_money"] = myPokemon.money;
			if(myPokemon.expToNextLevel <= 0) {
				myPokemon = levelUp(myPokemon, randPokemon.exp);
				localStorage["pt_level"] = myPokemon.level;
				localStorage["pt_expToNextLevel"] = myPokemon.expToNextLevel;
				localStorage["pt_strength"] = myPokemon.strength;
				localStorage["pt_speed"] = myPokemon.speed;
				localStorage["pt_hpmax"] = myPokemon.hpmax;
				localStorage.notifications = localStorage.notifications + "|Level Up!!!! You are now level " + localStorage.pt_level;
			}
		}
		else
		{
			localStorage.notifications = localStorage.notifications + "|You have lost a battle.";
		}
	}
}

function determineEncounter(){
	var rand = Math.random();
	console.log(rand);
	if(rand <= 0.5)
	{
		return true;
	}
	return false;
}